﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using System.Windows.Forms;
using Hdt.TransOcean.Dal;
using System.IO;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormCustomer : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        private EOperation _operation;
        public event System.Action NotifyAnotherForm2;
        //private Customer _customer;


        //public static EOperation Operation
        //{
        //    get { return _operation; }
        //    set { _operation = value; }
        //}


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
            hdtManageControl1.Width = this.Width;
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormCustomer(XafApplication application) : this() { }
        public FormCustomer()
        {
            InitializeComponent();
        }

        public FormCustomer(EOperation operation, string code = null)
        {
            InitializeComponent();
            this._operation = operation;
            txtCode.Enabled = false;
            Presentation.DisplayButtonByOperation(hdtManageControl1,operation);
            if (operation == EOperation.New)
            {
                txtCode.Text = Dao.GetAutoCode("Customer");
                chkUsed.Enabled = false;
            }
            if (operation == EOperation.Edit)
            {
                if (!String.IsNullOrEmpty(code))
                {
                    Customer customer = Dao.SelectCustomer(code)[0];

                    txtCode.Text = customer.code;
                    txtAddress.Text = customer.address;
                    txtName.Text = customer.name;
                    txtEmail.Text = customer.email;
                    txtDescription.Text = customer.description;
                    txtFax.Text = customer.fax;
                    txtDebtDayNum.Text = customer.debt_day_num.ToString();
                    rdoType.EditValue = customer.type;
                    txtMobile.Text = customer.mobile;
                    txtTaxCode.Text = customer.tax_code;
                    txtTelephone.Text = customer.telephone;
                    chkUsed.EditValue = customer.used;
                }
            }
        }

       

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            string message = "Lưu thành công!";
            //bool flag = true;
            Dao dao = new Dao();
            Customer customer = new Customer();
            customer.code = txtCode.Text;
            customer.name = txtName.Text;
            customer.address = txtAddress.Text;
            customer.telephone = txtTelephone.Text;
            customer.mobile = txtMobile.Text;
            customer.fax = txtFax.Text;
            customer.email = txtEmail.Text;
            customer.tax_code = txtTaxCode.Text;
            customer.debt_day_num = Int32.Parse(txtDebtDayNum.Text);
            customer.description = txtDescription.Text;
            customer.type = Int32.Parse(rdoType.Properties.Items[rdoType.SelectedIndex].Value.ToString());
            customer.used = 0;        
            try
            {
                if (Presentation.ValidCustomer(customer) == true)
                {
                    if(this._operation == EOperation.Edit)
                    {
                        dao.UpdateCustomer(customer);
                    }
                    else if(this._operation == EOperation.New)
                    {
                        if (Dao.InsertCustomer(customer))
                        {
                            //Presentation.ViewControl(this, EOperation.New);
                            AutoCode ac = new AutoCode();
                            ac.current_value = txtCode.Text.ToString().Substring(2,txtCode.Text.ToString().Length-2);
                            Dao.UpdateAutoCodeByTable(ac, "Customer");
                            ResetCustomerInfo();
                        }
                    }
                    Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Save);
                }
                
            }
            catch (Exception e)
            {
                if (e != null)
                {
                 //   flag = false;
                    message = e.ToString();
                }
            }
            finally
            {
                Common c = new Common();
                c.SetValueToTableUpdateString(EObject.Customer.ToString());

                MessageBox.Show(message, "Khách hàng - Lưu");
                //FormInvoice inv = new FormInvoice();
                //inv.LoadCustomer();
            }
        }

        private void hdtManageControl1_HdtExportButtonClick()
        {
            string [] titles = new string[] {"Mã", "Tên", "Địa chỉ", "Điện thoại", "Di động", "Fax", "Email", "Số ngày nợ", "Mã số thuế", "Ghi chú"};

            Dao.ExportExcel(EObject.Customer, "customer.xls", null);
            
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            try
            {
                Dao.ImportExcel(EObject.Customer);
            }
            catch
            {
                throw;
            }
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {
            txtCode.Enabled = false;
            ResetCustomerInfo();
            txtCode.Text = Dao.GetAutoCode("Customer");
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.New);
        }

        private void ResetCustomerInfo()
        {
            txtCode.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtDebtDayNum.Text = "0";
            txtDescription.Text = String.Empty;
            txtEmail.Text = String.Empty;
            txtFax.Text = String.Empty;
            txtMobile.Text = String.Empty;
            txtName.Text = String.Empty;
            txtTaxCode.Text = String.Empty;
            txtTelephone.Text = String.Empty;
            rdoType.EditValue = 1;
            chkUsed.EditValue = 0;
        }

        private void hdtManageControl1_HdtEditButtonClick()
        {
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Edit);
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            if (!String.IsNullOrEmpty(txtCode.Text))
            {
                try
                {
                    if (MessageBox.Show("Bạn muốn xóa không?", "Khách hàng", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        if (Dao.DeleteCustomerByCode(txtCode.Text) == true)
                        {
                            MessageBox.Show("Xóa thành công", "Khách hàng");
                            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Delete);
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }
            }
           
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }

    }
}
